import Api from '../../constants/Api'

export default {
  namespaced: true,
  state: {
    positionList: []
  },
  mutations: {
    positionListTo(state, data) {
      state.positionList = data
    }
  },
  actions: {
    getPositionList({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.getPositionList,
        data: {
          pTitle:that.pTitle,
          current: that.current,
          pageSize: that.pageSize
        }
      }).then(res => {
        commit('positionListTo', res.data.records)
        that.total = res.data.total
        that.tableLoading = false
      }).catch(() => {
        that.tableLoading = false
      })
    },
    getPositionById({commit}, that) {
      let url = Api.getPositionById.replace('{pId}', that.pId)
      return that.$axios({
        method: 'get',
        url: url
      }).then(res => {
        return res.data
      }).catch(() => {
        that.tableLoading = false
      })
    },
    addPosition({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.addPosition,
        data: that.positionForm
      }).then(res => {
        if (res.code === 20002) {
          that.handleVisible = false
          that.$message.success(res.msg)
          that.current = 1
          that.getData()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    editPosition({commit}, that) {
      that.$axios({
        method: 'post',
        url: Api.editPosition,
        data: that.positionForm
      }).then(res => {
        if (res.code === 20003) {
          that.handleVisible = false
          that.$message.success(res.msg)
          that.getData()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    removePosition({state}, that) {
      let url = Api.removePosition.replace('{pId}', that.pId)
      return that.$axios({
        method: 'get',
        url: url
      }).then(res => {
        if (res.code === 20004) {
          that.delVisible = false
          that.getData()
          that.$message.success(res.msg)
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    }
  }
}
